package com.aerfafish.spider.spider;

import com.aerfafish.spider.callback.SpiderCallback;
import com.aerfafish.spider.neo4j.dao.Person;
import com.aerfafish.spider.utils.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author aerfafish
 * @date 2021/4/19 10:44 上午
 */
public class ManagerSpider implements PageProcessor {


    private final static Logger logger = LoggerFactory.getLogger(ManagerSpider.class);

    private Spider spider = Spider.create(this).setExitWhenComplete(false).thread(5);

    private SpiderCallback spiderCallback;

    public ManagerSpider(SpiderCallback spiderCallback) {
        this.spiderCallback = spiderCallback;
        spider.start();
    }

    @Override
    public void process(Page page) {
        Person person = new Person();
        List<String> managerFundCode = new ArrayList<>();
        String code = page.getUrl().regex("(^http\\://fund\\.eastmoney\\.com/manager/)([0-9]*)(\\.html$)", 2).toString();
        person.setCode(code);
        String companyCode = page.getHtml().$("div.right.jd")
                .links().regex("(^http\\://fund\\.eastmoney\\.com/company/)([0-9]*)(\\.html$)", 2).toString();
        Selectable links = page.getHtml().$("div.right.jd")
                .links();
        List<Selectable> nodes = page.getHtml().$("table.ftrs").nodes();
        for (Selectable node : nodes) {
            if (node.$("caption").toString().contains("现任基金业绩与排名详情")) {
                List<Selectable> funds = node.$("tbody").$("tr").nodes();
                for (Selectable fund : funds) {
                    managerFundCode.add(fund.links().regex("(^http\\://fund\\.eastmoney\\.com/)([0-9]*)(\\.html$)", 2).toString());
                }
                break;
            }
        }
        spiderCallback.onSavePerson(person);
        spiderCallback.onSavePersonCompanyRelation(code, companyCode);

    }

    @Override
    public Site getSite() {
        return Site.me().setCycleRetryTimes(3000).setSleepTime(10).setCharset("UTF-8").setTimeOut(20000)
                .setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
    }

    public void spiderManager(String managerCode) {
        spider.addRequest(RequestUtil.urlToRequest(
                String.format("http://fund.eastmoney.com/manager/%s.html", managerCode),
                "http://fundf10.eastmoney.com/"));
    }
}
